package com.weimob.kafka;

import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * @program: xh-learn-record
 * @description:
 * @author: sch
 * @create: 2022-12-20 18:42
 **/
public class A006_CustomProducerCallbackPartitions {
    public static void main(String[] args) {
        //1,创建生产者
        Properties properties = new Properties();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.1.3:9092,192.168.1.3:9093,192.168.1.3:9094");
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

        //使用自定义分区器
//        properties.put(ProducerConfig.PARTITIONER_CLASS_CONFIG, A005_MyPartitioner.class.getName());

        KafkaProducer kafkaProducer = new KafkaProducer(properties);

        //发送消息
        for (int i = 0; i < 10; i++) {
            kafkaProducer.send(new ProducerRecord("first", "hello" + i), new Callback() {
                public void onCompletion(RecordMetadata metadata, Exception exception) {
                    System.out.println("topic:" + metadata.topic() + ",分区:" + metadata.partition());
                }
            });
        }

        //关闭资源
        kafkaProducer.close();
    }
}
